<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output
        encoding="utf-8"
        indent="no"
        media-type="text/xml"
        omit-xml-declaration="yes"
        standalone="no" />

    <xsl:param name="cmd" />

    <xsl:include href="./inc/App.xslt"/>
    <xsl:include href="./inc/Form.xslt"/>

    <xsl:template match="/">
        <div>
        <xsl:choose>
            <xsl:when test="/InternalMessage"><xsl:call-template name="app_message" /></xsl:when>
            <xsl:when test="($cmd='GetFragmentForEdit') or ($cmd='InsertFragment') or ($cmd='NewFragment') or ($cmd='SetFragment')"><xsl:call-template name="form" /></xsl:when>
            <xsl:otherwise>
                <h1>Unknown Input</h1>
                <p>Command: <xsl:value-of select="$cmd"/></p>
                <p>Server Response:</p>
                <p><textarea id="RawServerResponse"><xsl:copy-of select="/" /></textarea></p>
            </xsl:otherwise>
        </xsl:choose>
        </div>
    </xsl:template>

    <xsl:template name="form">

        <div class="Branch">
            <xsl:for-each select="//FragmentBranch/*">
                <xsl:choose>
                    <xsl:when test="local-name(.) = 'Document'">
                    <xsl:variable name="branch_document_name">
                        <xsl:choose>
                            <xsl:when test="string-length(./Title) &gt; 0">
                                Document: <xsl:value-of select="./Title"/>
                            </xsl:when>
                            <xsl:otherwise>
                                Document[<xsl:value-of select="./DocumentId"/>]
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
            <a class="DocumentLink" href="#" id="{concat('item_',position(),'a_',./DocumentId)}" title="{./Title}"><xsl:value-of select="$branch_document_name"/></a><span class="BranchDelimiter">/</span>
                    </xsl:when>
                    <xsl:when test="local-name(.) = 'Segment'">
                    <xsl:variable name="branch_segment_name">
                        <xsl:choose>
                            <xsl:when test="string-length(./SegmentName) &gt; 0">
                                <xsl:value-of select="./SegmentName"/>
                            </xsl:when>
                            <xsl:otherwise>
                                Segment[<xsl:value-of select="./SegmentId"/>]
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
            <a class="SegmentLink" href="#" id="{concat('item_',position(),'a_',./SegmentId)}" title="{./SegmentName}"><xsl:value-of select="$branch_segment_name"/></a><span class="BranchDelimiter">/</span>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </div>
        <div class="Branch">
            <a class="FragmentLink" href="#" id="{concat('item_',position(),'a_',/ServerData/Fragment/FragmentId)}" title="{concat(/ServerData/Fragment/FragmentDisplayName, ' ', /ServerData/Fragment/FragmentName, ' ', /ServerData/Fragment/ClientId)}">Fragment</a><span class="BranchDelimiter">/</span>
        </div>

        <xsl:choose>
            <xsl:when test="(string-length(/ServerData/Fragment/FragmentId) &gt; 0) and (string-length(/ServerData/Fragment/FragmentName) &gt; 0)">
        <h1 class="DataEntryForm"><xsl:copy-of select="/ServerData/Fragment/FragmentName"/></h1>
            </xsl:when>
            <xsl:when test="(string-length(/ServerData/Fragment/FragmentId) &gt; 0) and (string-length(/ServerData/Fragment/FragmentDisplayName) &gt; 0)">
        <h1 class="DataEntryForm"><xsl:copy-of select="/ServerData/Fragment/FragmentDisplayName"/></h1>
            </xsl:when>
            <xsl:otherwise>
        <h1 class="DataEntryForm">New Fragment</h1>
            </xsl:otherwise>
        </xsl:choose>

        <form action="#" class="DataEntryForm" id="FragmentForm">
            <div class="CalendarBlock" id="CalendarBlock">&#160;</div>
            <div id="DataFormTabView" class="yui-navset">
                <ul class="yui-nav">
                    <li class="selected"><a class="TabAnchor" href="#ItemOne"><em>General</em></a></li>
                    <li><a class="TabAnchor" href="#ItemTwo"><em>Advanced</em></a></li>
                    <li><a class="TabAnchor" href="#ItemThree"><em>Prose</em></a></li>
                </ul>
                <div class="yui-content">
                    <div id="ItemOne">
                        <xsl:if test="string-length(/ServerData/Fragment/FragmentId) &gt; 0"><xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/FragmentId" /><xsl:with-param name="caption" select="'Fragment ID'"/><xsl:with-param name="type" select="'data_id'" /></xsl:call-template></xsl:if>
                        <xsl:call-template name="form_row">
                            <xsl:with-param name="n" select="//Documents/Document"/>
                            <xsl:with-param name="type" select="'option'" />
                            <xsl:with-param name="maxlength" select="32" />
                            <xsl:with-param name="option_display_name" select="'FileName'" />
                            <xsl:with-param name="option_select_id" select="'DocumentId'" />
                            <xsl:with-param name="option_selected_value" select="/ServerData/Fragment/DocumentId" />
                            <xsl:with-param name="option_value"  select="'DocumentId'"/>
                            <xsl:with-param name="caption" select="'Document'"/>
                        </xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/FragmentName" /><xsl:with-param name="caption" select="'Fragment Name'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/FragmentDisplayName" /><xsl:with-param name="type" select="'textarea_node'" /><xsl:with-param name="caption" select="'Fragment Display Name'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/CreateDate"/><xsl:with-param name="caption" select="'Publication Date'"/><xsl:with-param name="type" select="'datepicker'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/ModificationDate"/><xsl:with-param name="caption" select="'Modification Date'"/><xsl:with-param name="type" select="'datepicker'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/EndDate"/><xsl:with-param name="caption" select="'Expiration Date'"/><xsl:with-param name="type" select="'datepicker'" /></xsl:call-template>
                    </div>
                    <div id="ItemTwo">
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/IsActive" /><xsl:with-param name="caption" select="'Fragment Active?'"/><xsl:with-param name="type" select="'checkbox'" /></xsl:call-template>
                        <xsl:call-template name="form_row">
                            <xsl:with-param name="n" select="//Fragments/Fragment"/>
                            <xsl:with-param name="type" select="'option'" />
                            <xsl:with-param name="maxlength" select="32" />
                            <xsl:with-param name="option_display_name" select="'DisplayName'" />
                            <xsl:with-param name="option_select_id" select="'PrevFragmentId'" />
                            <xsl:with-param name="option_selected_value" select="ServerData/Fragment/PrevFragmentId" />
                            <xsl:with-param name="option_value"  select="'FragmentId'"/>
                            <xsl:with-param name="caption" select="'Previous Fragment'"/>
                        </xsl:call-template>
                        <xsl:call-template name="form_row">
                            <xsl:with-param name="n" select="//Fragments/Fragment"/>
                            <xsl:with-param name="type" select="'option'" />
                            <xsl:with-param name="maxlength" select="32" />
                            <xsl:with-param name="option_display_name" select="'DisplayName'" />
                            <xsl:with-param name="option_select_id" select="'NextFragmentId'" />
                            <xsl:with-param name="option_selected_value" select="ServerData/Fragment/NextFragmentId" />
                            <xsl:with-param name="option_value"  select="'FragmentId'"/>
                            <xsl:with-param name="caption" select="'Next Fragment'"/>
                        </xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/IsPrevious" /><xsl:with-param name="caption" select="'Header?'"/><xsl:with-param name="type" select="'checkbox'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/IsNext" /><xsl:with-param name="caption" select="'Footer?'"/><xsl:with-param name="type" select="'checkbox'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/IsWrapper" /><xsl:with-param name="caption" select="'Wrapper?'"/><xsl:with-param name="type" select="'checkbox'" /></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/SortOrdinal" /><xsl:with-param name="caption" select="'Sort Ordinal'"/><xsl:with-param name="maxlength" select="'4'"/></xsl:call-template>
                        <xsl:call-template name="form_row"><xsl:with-param name="n" select="/ServerData/Fragment/ClientId" /><xsl:with-param name="caption" select="'Client ID'"/></xsl:call-template>
                    </div>
                    <div id="ItemThree">
                        <textarea id="ProseFragment">
                            <xsl:choose>
                                <xsl:when test="string-length(/ServerData/Fragment/ItemChar) &gt; 0"><xsl:copy-of select="/ServerData/Fragment/ItemChar/node()" /></xsl:when>
                                <xsl:when test="string-length(/ServerData/Fragment/ItemText) &gt; 0"><xsl:copy-of select="/ServerData/Fragment/ItemText/node()" /></xsl:when>
                                <xsl:otherwise>&#160;</xsl:otherwise>
                            </xsl:choose>
                        </textarea>
                    </div>
                </div>
            </div>
            <div class="EditCommands">
                <button type="reset">Cancel</button>
                <xsl:if test="string-length(/ServerData/Fragment/FragmentId) &gt; 0"><button id="DeleteFragment">Delete</button></xsl:if>
                <xsl:variable name="save_id">
                <xsl:choose>
                    <xsl:when test="string-length(/ServerData/Fragment/FragmentId) &gt; 0">SaveFragment</xsl:when>
                    <xsl:otherwise>SaveNewFragment</xsl:otherwise>
                </xsl:choose>
                </xsl:variable>
                <button id="{$save_id}">Save</button>
            </div>
        </form>
    </xsl:template>

</xsl:stylesheet> 
